package fons.navigator.ui.actions;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import net.java.dev.designgridlayout.DesignGridLayout;
import fons.model.ontology.language.Sentence;
import fons.model.ontology.language.Vocabulary;
import fons.model.query.Analyzer;
import fons.model.query.Answer;
import fons.navigator.NavigatorMain;
import fons.navigator.ui.NavigatorMainFrame;
import fons.navigator.util.SwingUtils;

@ActionDescription(
		menu = "Ontologie",
		name = "Ontologie-Abfrage", description = "Fragen zu den Daten des aktuell aktiven Kontext stellen",
		priority = 2
		)
public class QueryAction extends NavigatorAbstractAction {
	private static final long serialVersionUID = 1L;

	public QueryAction(NavigatorMainFrame app) {
		super(app, false);
	}
	
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		QueryDialog queryDialog = new QueryDialog("Ontologie-Abfrage", getApp());
		queryDialog.pack();
		queryDialog.setVisible(true);
	}
	
	
	public static class QueryDialog extends JDialog {
		private static final long serialVersionUID = 1L;
		private static final String ASK_BUTTON_ACTION="ask";
		private static final String SAVE_BUTTON_ACTION="save";
		private final Vocabulary vocabulary;
		
		private final DesignGridLayout layout;
		
		private Answer answer;
		
		public void setAnswer(Answer answer) {
			this.answer = answer;
		}
		
		public Answer getAnswer() {
			return answer;
		}
		
		private QueryDialog (String title, Container parent) {			
			super(SwingUtils.getWindowFor(parent), title, ModalityType.MODELESS);
			this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

			NavigatorMain.getMainFrame().getActiveContext().buildVocabulary();
			this.vocabulary = NavigatorMain.getMainFrame().getActiveContext().getVocabulary();
			
			layout = new DesignGridLayout(this);
					
			final JTextField textField = new JTextField(50);
			final JEditorPane answerField = new JEditorPane("text/html", 
					"Antwort: Stellen Sie eine Frage und klicken Sie auf OK");
			answerField.setEditable(false);

			setPreferredSize(new Dimension(960, 760));
			setResizable(false);
			
			final JScrollPane scrollPane = new JScrollPane(answerField,
					JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
					JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
						
			final JButton ok = new JButton("OK");
			ok.setActionCommand(ASK_BUTTON_ACTION);
			ok.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					if (ASK_BUTTON_ACTION.equals(arg0.getActionCommand())) {
						final String question = textField.getText();
							
						Analyzer analyzer = new Analyzer(NavigatorMain.getMainFrame().getActiveContext());
						Answer answer = analyzer.analyze(new Sentence(question), vocabulary);
						setAnswer(answer);
						answerField.setText("<html><body>" + answer + "</html></body>");
					}
				}
			});
			

			final JButton save = new JButton("Als Excel-Datei speichern");
			save.setActionCommand(SAVE_BUTTON_ACTION);
			save.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					if (SAVE_BUTTON_ACTION.equals(e.getActionCommand())) {
						FileDialog fileChooser = new FileDialog(QueryDialog.this, "Speichern", FileDialog.SAVE);
						fileChooser.setVisible(true);				
						
						
						String filename = fileChooser.getDirectory() + '/' + fileChooser.getFile();
						if (!fileChooser.getFile().endsWith(".xls")) {
							filename = filename += ".xls";
						}
						getAnswer().saveAs(filename);
					}					
				}
			});
			
			
			layout.row().grid().add(new JLabel("<html><body>" +
					"Stellen Sie eine Frage!<br/>" + "</body></html>"));
			layout.row().grid().add(textField, 2);
			layout.row().grid().add(ok).empty(1).add(save);
			layout.row().grid().add(scrollPane);
			
			final Window window = SwingUtils.getWindowFor(parent);
			this.setLocationRelativeTo(window);
			this.setLocation(0, 0);
		}		
	}
}
